﻿using Prism.Commands;
using Prism.Regions;
using SmartParking.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SmartParking.ViewModels.Pages
{
    public class ViewModelBase<T> : INavigationAware
    {
        public string PageTitle { get; set; } = "页面标题";
        public bool IsCanClose { get; set; } = true;
        public string SearchKey { get; set; }
        public DelegateCommand CloseCommand { get; set; }
        public DelegateCommand RefreshCommand { get; set; }
        public DelegateCommand<T> ModifyCommand { get; set; }
        public DelegateCommand<T> DeleteCommand { get; set; }

        IRegionManager _regionManager;
        public ViewModelBase(IRegionManager regionManager)
        {
            _regionManager = regionManager;

            CloseCommand = new DelegateCommand(DoClose);
            RefreshCommand = new DelegateCommand(Refresh);
            ModifyCommand = new DelegateCommand<T>(DoModify);
            DeleteCommand = new DelegateCommand<T>(DoDelete);
        }

        private void DoClose()
        {
            //获取主区域，从主区域获取views，根据页面对应view名称，移除view
            var region = _regionManager.Regions["MainRegion"];

            var view = region.Views.FirstOrDefault(t => t.GetType().Name == PageName);
            if (view != null)
            {
                region.Remove(view);
            }
        }

        private string PageName = "";
        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            PageName = navigationContext.Uri.ToString();
        }

        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return true;
        }

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {
            
        }

        public virtual void Refresh()  {   }
        public virtual void DoModify(T model) { }
        public virtual void DoDelete(T model) { }
    }
}
